package com.dominos.voldemortstress;

import java.util.Date;

import org.apache.log4j.Logger;

public class ReadThread implements Runnable {

	Logger logger = Logger.getLogger(this.getClass());
	boolean done = false;
	boolean quit = false;
	int readCount = 0;
	int errorCount = 0;
	long duration = 0;
	private Object synchronizedForStats = new Object();
	private String key;

	private VoldemortStoreClient storeClient;

	public ReadThread(VoldemortStoreClient voldemortStoreClient,
			int readThreadIndex) {
		this.storeClient = voldemortStoreClient;
		this.key = "ReadTestKey" + readThreadIndex;
	}

	@Override
	public void run() {
		quit = false;
		storeClient.put(key, "Some Data");
		while (!done) {
			synchronized (synchronizedForStats) {
				readCount++;
				long startTime = new Date().getTime();
				try {
					storeClient.get(key);
				} catch (Exception e) {
					errorCount++;
					logger.error(e.getMessage());
				}
				long endTime = new Date().getTime();
				long difference = endTime - startTime;
				duration += difference;
			}
		}

		quit = true;
	}

	public void quit() {
		done = true;
	}

	public boolean isQuit() {
		return quit;
	}

	public int getReadCount() {
		synchronized (synchronizedForStats) {
			return readCount;
		}
	}

	public int getErrorCount() {
		synchronized (synchronizedForStats) {
			return errorCount;
		}
	}

	public long getDuration() {
		synchronized (synchronizedForStats) {

			return duration;
		}
	}



}
